package top.callname.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;

import top.callname.bean.ClassBean;
import top.callname.bean.ClassPageList;


public class ClassDao extends BaseDao{

	
	Logger logger = Logger.getLogger(ClassDao.class);
	
	
	public boolean insertClass(ClassBean bean){
		String sql = "insert into t_class (class_name,createtime) values ( '"+bean.getClassName()+"',NOW() )";
		logger.info("添加班级："+sql);
		return adu(sql);
	}
	
	public boolean deleteByCid(Integer cid){
		String sql = "delete from t_class where cid="+cid;
		logger.info("删除班级信息："+sql);
		return adu(sql);
	}
	
	public ClassPageList selectByPage(int pageNow){
		ClassPageList pageList = new ClassPageList();
		pageList.setPageNow(pageNow);
		//查询总记录
		int totalSize = getCount("t_class");
		pageList.setTotalSize(totalSize);
		
		ArrayList<ClassBean> list = new ArrayList<ClassBean>();
		
		String limit = "order by cid desc limit "+pageList.getStart()+","+pageList.getOffset();
		String sql =  "select  cid,class_name,createtime from t_class "+limit;
		
		logger.info("分页查询班级信息："+sql);
		res = query(StringEscapeUtils.escapeSql(sql));
		try {
			while(res.next()){
				ClassBean bean = new ClassBean();
				bean.setCid(res.getInt("cid"));
				bean.setClassName(res.getString("class_name"));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				list.add(bean);
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_class 出错："+e);
		}
		pageList.setList(list);
		return pageList;
	}
	
	public ClassBean selectByCid(Integer cid){
		String sql =  "select  cid,class_name,createtime"
				+ "  from t_class where cid="+cid;
		res = query(StringEscapeUtils.escapeSql(sql));
		ClassBean bean = new ClassBean();
		try {
			if(res.next()){
				bean.setCid(res.getInt("cid"));
				bean.setClassName(res.getString("class_name"));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_class 出错："+e);
		}
		return bean;
	}
	
	public List<ClassBean> selectAll(){
		ArrayList<ClassBean> list = new ArrayList<ClassBean>();
		String sql =  "select  cid,class_name,createtime  from t_class ";
		res = query(StringEscapeUtils.escapeSql(sql));
		try {
			while(res.next()){
				ClassBean bean = new ClassBean();
				bean.setCid(res.getInt("cid"));
				bean.setClassName(res.getString("class_name"));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				list.add(bean);
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_class 出错："+e);
		}
		return list;
	}
	
	public List<ClassBean> selectByCids(String cids){
		ArrayList<ClassBean> list = new ArrayList<ClassBean>();
		String sql =  "select  cid,class_name,createtime"
				+ "  from t_class where cid in( " +cids+" )";
		res = query(StringEscapeUtils.escapeSql(sql));
		try {
			while(res.next()){
				ClassBean bean = new ClassBean();
				bean.setCid(res.getInt("cid"));
				bean.setClassName(res.getString("class_name"));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				list.add(bean);
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_class 出错："+e);
		}
		return list;
	}
	/**
	 * 
	 * Description:根据登录人信息查询
	 * @param pid
	 * @return List<ClassBean>
	 */
	public List<ClassBean> selectByPid(Integer pid){
		ArrayList<ClassBean> list = new ArrayList<ClassBean>();
		String sql =  "select  cid,class_name,createtime"
				+ "  from t_class where cid in( SELECT cid from t_admin_class where pid="+pid+")";
		res = query(StringEscapeUtils.escapeSql(sql));
		try {
			while(res.next()){
				ClassBean bean = new ClassBean();
				bean.setCid(res.getInt("cid"));
				bean.setClassName(res.getString("class_name"));
				String date = res.getString("createtime");
				bean.setCreatetime(date.substring(0,date.length()-2));
				list.add(bean);
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_class 出错："+e);
		}
		return list;
	}
	
	
	
}
